package com.myflink.day03;

import org.apache.flink.api.common.functions.RuntimeContext;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.connectors.elasticsearch.ElasticsearchSinkFunction;
import org.apache.flink.streaming.connectors.elasticsearch.RequestIndexer;
import org.apache.flink.streaming.connectors.elasticsearch6.ElasticsearchSink;
import org.apache.http.HttpHost;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.Requests;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author Shelly An
 * @create 2020/9/18 11:43
 */
public class Sink_ElasticSearch {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        //环境来产生数据流
        DataStreamSource<String> fileDS = env.readTextFile("input/sensor-data.log");

        //访问es
        List<HttpHost> httpHosts = new ArrayList<>();
        httpHosts.add(new HttpHost("hadoop102", 9200));
        httpHosts.add(new HttpHost("hadoop103", 9200));
        httpHosts.add(new HttpHost("hadoop104", 9200));


        ElasticsearchSink<String> esSink = new ElasticsearchSink.Builder<String>(
                //9300 tcp 内部通信 9200 http 外部访问
                httpHosts,
                new ElasticsearchSinkFunction<String>() {

                    @Override
                    public void process(String element, RuntimeContext ctx, RequestIndexer indexer) {
                        //将数据放在map中
                        HashMap<String, String> dataMap = new HashMap<>();
                        dataMap.put("data", element);
                        //创建indexRequest 指定index、type、source
                        IndexRequest indexRequest = Requests.indexRequest("sensor").type("reading").source(dataMap);
                        //到RequestIndexer
                        indexer.add(indexRequest);
                    }
                }

        ).build();

        


        env.execute();
    }
}
